// pages/detail/detail.js
const host=getApp().host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    host:'',
    num:10,// 当前已经报名人数
    status:false,
    current:''
  },

  /**点击取消 */
  handleCancel(){
    wx.showLoading({
      title: '加载中',
    })
    let loginName=wx.getStorageSync('admin').loginName;
    wx.request({
      url: host+'/client/enlists/cancel?user='+loginName+'&contest='+this.data.info.id,
      success:()=>{
        this.initRegNum(this.data.info.id);
        this.initStatus(this.data.info.id);
        wx.hideLoading();
      }
    })
  },

  /**点击报名 */
  handleReg(){
    if(
      this.data.info.max>this.data.num 
      && 
      new Date().getTime()<this.data.info.time
      &&
      this.data.info.status==1
      ){
      wx.showLoading({
        title: '报名中',
      })
      let enlists={
        user:wx.getStorageSync('admin').loginName,
        contest:this.data.info.id,
        status:1
      };
      wx.request({
        url: host+'/client/enlists/add',
        method:'POST',
        data:enlists,
        header:{
          'content-type': 'application/x-www-form-urlencoded'
        },
        success:()=>{
          this.initRegNum(this.data.info.id);
          this.initStatus(this.data.info.id);
          wx.hideLoading();
        }
      })
    }else{
      wx.showToast({
        title: '报名失败',
        icon:'error'
      })
    }
  },

  /**点击打电话 */
  handlePhone(){
    wx.makePhoneCall({
      phoneNumber: this.data.info.contact,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: host+'/client/contest/info?id='+options.id,
      success:res=>{
        this.initRegNum(options.id);
        this.initStatus(options.id);

        let info=res.data.data.info;
        info.date=this.formatDate(info.date);
        info.endTime=this.formatDate(info.time);
        this.setData({
          info:info,
          host:host,
          current:new Date().getTime()
        },()=>{
          wx.hideLoading();
        })
      }
    })
  },

  /**初始化报名人数 */
  initRegNum(id){
    wx.request({
      url: host+'/client/enlists/list?page=0&pageSize=1000&contest='+id,
      success:res=>{
        this.setData({
          num:res.data.data.total
        })
      }
    })
  },

  /**初始化当前用户有没有报名 */
  initStatus(id){
    let loginName=wx.getStorageSync('admin').loginName;
    wx.request({
      url: host+'/client/enlists/status?user='+loginName+'&contest='+id,
      success:res=>{
        this.setData({
          status:res.data.data.status
        })
      }
    })
  },

  /**格式化时间 */
  formatDate(row) {
    let date = new Date(row);
    let month =
          date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1;
    let day =
          date.getDate() < 10
                ? "0" + date.getDate()
                : date.getDate();
    let hours =
          date.getHours() < 10
                ? "0" + date.getHours()
                : date.getHours();
    let minutes =
          date.getMinutes() < 10
                ? "0" + date.getMinutes()
                : date.getMinutes();
    return (
          date.getFullYear() +
          "-" +
          month +
          "-" +
          day +
          " " +
          hours +
          ":" +
          minutes
    );
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})